package algorithm;

public class SuperPalindormes {
    public static void main(String[] args) {
        System.out.println("有"+new Ssp().superpalindromesInRange("1","5000000000000000000")+"个超级回文数");
    }

}

class Ssp {
    public int superpalindromesInRange(String left, String right) {
        Long l = Long.parseLong(left);
        Long r = Long.parseLong(right);
        int res = 0;
        long number = 1;
        while(true){
            long cn = number;
            long cnr = 0;
            String pr = "";
            while(cn>0){
                cnr=cn%10;
                pr+=cnr;
                cn=cn/10;
            }
            long cnn = Long.parseLong(number+pr.substring(1));
            Long cn2 = cnn*cnn;
            if(cn2>r)
                break;
            if(cn2>=l&&check(cn2+"")) {
                res++;
                System.out.println(cn2);
            }
            cnn = Long.parseLong(number+""+pr);
            cn2 = cnn*cnn;
            if(cn2>=l&&cn2<=r&&check(cn2+"")){
                res++;
                System.out.println(cn2);
            }
            number++;
        }
        return res;
    }
    boolean check(String s){
        int l = 0;
        int r = s.length()-1;
        while(l<r){
            if(s.charAt(l)!=s.charAt(r))
                return false;
            l++;
            r--;
        }
        return true;
    }
}
